import React, { Component } from 'react';
import axios from 'axios'
class Captcha extends Component {
    state = {
        img:""
    }
    render() {
        return (
            <div>
                {/* 接受来自父组件高度的指定 */}
                <img src={this.state.img} alt="captcha" height={this.props.height} onClick={this.loadCaptcha}/>
            </div>
        );
        
    }
    //发起网络请求
        componentDidMount () {
            this.loadCaptcha()
        }
        //
        loadCaptcha=()=>{
            axios.get('https://reactapi.iynn.cn/captcha/api/math').then(req=>{
                console.log(req);
                //将验证码赋值给img
                this.setState(()=>{
                    return {
                        img:req.img
                    }
                })
                //把key传给父组件
                //父组件
                this.props.setKey(req.key)
            }
            )
        }

}

export default Captcha;
